import Request from '../../js_sdk/luch-request/luch-request/index.js'
import config from "./config.service.js"

const getTokenStorage = () => {
	let token = ''
	try{
		token = uni.getStorageSync('ACCESS_TOKEN')
		
	}catch(e){
		//TODO handle the exception
		console.log("getTokenStorage",token)
	}
	return token
}

let apiUrl = config.baseURL

const http = new Request()
http.setConfig((config)=>{
	config.baseURL = apiUrl
	config.header = {
		...config.header
	}
	return config
})


http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  config.header = {
    ...config.header,
    'X-Access-Token':getTokenStorage()
  }
  return config
}, config => {
  return Promise.reject(config)
})


http.interceptors.response.use( (response) =>{ /* 对响应成功做点什么 可使用async await 做异步操作*/
	
	return response
	
}, (response)=>{		/*  对响应错误做点什么 （statusCode !== 200）*/
	
	// if(response){
		
	// }
	
	return response
})

export {
	http
}
